package com.sunlee.admin.api.module.mall.controller;

import com.sunlee.admin.api.annotation.LogOperation;
import com.sunlee.admin.api.util.ExcelUtils;
import com.sunlee.common.core.entity.PageData;
import com.sunlee.common.core.entity.Result;
import com.sunlee.common.core.utils.AssertUtils;
import com.sunlee.common.core.validator.group.AddGroup;
import com.sunlee.common.core.validator.group.DefaultGroup;
import com.sunlee.common.core.validator.group.UpdateGroup;
import com.sunlee.common.service.dto.AboutConfigDTO;
import com.sunlee.common.service.excel.AboutConfigExcel;
import com.sunlee.common.service.service.AboutConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 关于页面配置表
 *
 * @author sunlee
 */
@RestController
@RequestMapping("aboutConfig")
@Api(tags = "关于页面配置表")
public class AboutConfigController {
    @Autowired
    private AboutConfigService aboutConfigService;

    @GetMapping("page")
    @ApiOperation("分页")
    @RequiresPermissions("aboutConfig:page")
    public Result<PageData<AboutConfigDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        return Result.ok(aboutConfigService.pageOwn(params));
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("aboutConfig:info")
    public Result<AboutConfigDTO> get(@PathVariable("id") Long id) {
        return Result.ok(aboutConfigService.getOwn(id));
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("aboutConfig:save")
    public Result<Object> save(@Validated({AddGroup.class, DefaultGroup.class}) @RequestBody AboutConfigDTO dto) {
        aboutConfigService.saveOwn(dto);
        return Result.ok();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("aboutConfig:update")
    public Result<Object> update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody AboutConfigDTO dto) {
        aboutConfigService.updateOwn(dto);
        return Result.ok();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("aboutConfig:delete")
    public Result<Object> delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        aboutConfigService.deleteOwn(ids);
        return Result.ok();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("aboutConfig:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) {
        List<AboutConfigDTO> list = aboutConfigService.listOwn(params);

        ExcelUtils.exportExcelToTarget(response, null, list, AboutConfigExcel.class);
    }

}
